Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Connection to be interrupted #343

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rebasedming
Copy link

Closes #342

Allows the Connection to be interrupted, which is useful for cancelling long-running queries.

Copy link
Member

@Mause Mause left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please get some tests and docs added? And remove the dsstore files

@rebasedming
Copy link
Author

Can we please get some tests and docs added? And remove the dsstore files

Yep! Put this up hastily to see if there was appetite for this functionality. I can clean it up now and add tests if there is.

@philippemnoel philippemnoel deleted the interrupt-connection branch July 9, 2024 18:15
@Mytherin
Copy link
Contributor

Mytherin commented Jul 10, 2024

Could you merge this with main, and perhaps also add a test for this functionality?

@rebasedming rebasedming restored the interrupt-connection branch July 12, 2024 21:01
@rebasedming
Copy link
Author

Yes, I got distracted with other things the last few weeks. I'll finish up this PR.

@rebasedming
Copy link
Author

rebasedming commented Jul 12, 2024

Hey @Mause @Mytherin, I could use your input on how you'd like to see this tested. What I'm envisioning is spawning a long-running query in one thread, and then interrupting the same connection from another thread. Unfortunately, once a Connection is moved to a thread, it can't really be called from another thread. For instance, the following code won't compile because db is moved to the thread and you can't call interrupt on it afterward.

  #[test]
  fn test_interrupt() -> Result<()> {
      let db = checked_memory_handle();

      let query_handle = std::thread::spawn(move || {
          let mut stmt = db.prepare("CREATE TABLE foo AS SELECT generate_series(1, 10000000) AS id;")?;
          stmt.execute([])?;
          Ok(())
      });

      db.interrupt();

      let query_result: Result<(), Error> = query_handle.join().unwrap();
      assert!(query_result.is_err());
      Ok(())
  }

@Mytherin
Copy link
Contributor

That idea for a test sounds good to me.

Unfortunately, once a Connection is moved to a thread, it can't really be called from another thread. For instance, the following code won't compile because db is moved to the thread and you can't call interrupt on it afterward.

I'm not too familiar with Rust specifics - but how do you envision this method to be used in practice if this is not possible? Maybe there needs to be some annotation that signifies it is safe to call interrupt from a different thread?

@rebasedming
Copy link
Author

rebasedming commented Jul 13, 2024

That idea for a test sounds good to me.

Unfortunately, once a Connection is moved to a thread, it can't really be called from another thread. For instance, the following code won't compile because db is moved to the thread and you can't call interrupt on it afterward.

I'm not too familiar with Rust specifics - but how do you envision this method to be used in practice if this is not possible? Maybe there needs to be some annotation that signifies it is safe to call interrupt from a different thread?

In practice, I've stored the Connection in a static global. I can do the same for the tests if you're comfortable with it?

@Mytherin
Copy link
Contributor

If the tests are completely stand-alone I don't mind - but ideally we don't ship any static globals. Is there no other way?

@philippemnoel philippemnoel deleted the interrupt-connection branch July 22, 2024 21:14
@philippemnoel philippemnoel restored the interrupt-connection branch July 24, 2024 18:17
@philippemnoel philippemnoel deleted the interrupt-connection branch December 2, 2024 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot use duckdb_interrupt due to private InnerConnection
3 participants